package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDTO;
import com.zzyl.dto.NursingProjectPlanDTO;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVO;

import com.zzyl.vo.NursingProjectPlanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class NursingProjectPlanServiceImpl implements NursingProjectPlanService {
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Override
    public PageResponse<NursingPlanVO> selectPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> nursingProjectPlanPageInfo = nursingProjectPlanMapper.selectPage(name, status);
        return PageResponse.of(nursingProjectPlanPageInfo, NursingPlanVO.class);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void add(NursingPlanDTO NursingPlandto) {

        // 插入护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(NursingPlandto, NursingPlan.class);
        nursingProjectPlanMapper.insert(nursingPlan);

        // 先获取关联表的数据集合
        List<NursingProjectPlan> nursingPlans = nursingPlan.getProjectPlans();
        // 如果集合不为空，则循环设置护理计划ID
        if (!CollectionUtils.isEmpty(nursingPlans)) {
            nursingPlans.forEach(  nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
            });
        }
        // 插入护理项目计划
        nursingPlanMapper.insert(nursingPlans);
    }

    @Override
    public NursingPlanVO getById(Long id) {
        NursingPlan nursingProjectPlan1 = nursingProjectPlanMapper.selectById(id);
        return BeanUtil.toBean(nursingProjectPlan1, NursingPlanVO.class);
    }


    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(NursingPlanDTO nursingPlandto) {
        // 先更新护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlandto, NursingPlan.class);
        nursingProjectPlanMapper.updateById(nursingPlan);


        // 先删除关联表的数据
        nursingPlanMapper.deleteByPlanId(nursingPlan.getId());

        // 再插入关联表的数据
        List<NursingProjectPlan> nursingPlans = nursingPlan.getProjectPlans();
        if (!CollectionUtils.isEmpty(nursingPlans)) {
            nursingPlans.forEach(  nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
            });
        }
        nursingPlanMapper.insert(nursingPlans);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(Long id) {
        nursingPlanMapper.deleteByPlanId(id);
        nursingProjectPlanMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        nursingProjectPlanMapper.updateById(nursingPlan);
    }

    @Override
    public List<NursingPlanVO> select() {
        List<NursingPlan> nursingPlans = nursingProjectPlanMapper.selectList();
        return BeanUtil.copyToList(nursingPlans, NursingPlanVO.class);
    }
}
